/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainForm.java
 *
 * Created on 6 janv. 2011, 15:08:06
 */
package com.bash.compta.gui;

//import com.bash.jcouture.util.MySwingUtilities;
import com.bash.compta.data.CreditLevel;
import com.bash.compta.data.Login;
import com.bash.compta.db4o.DBTool;
import com.bash.compta.db4o.MagicData;
import com.bash.jcouture.util.swing.MySwingUtilities;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import javax.swing.UIManager;
import org.jdesktop.swingx.JXHyperlink;

/**
 *
 * @author bashizip
 */


public class MainForm extends javax.swing.JFrame {

    public static Login login;

    private JXHyperlink hynewclient, hydaillyreports,
            hyclientsList,
            hyHistoriqueDepenses, hylogout, hycategorie,
            hycomptes, hybalance, hytaux,hy_recap;


    private MySwingUtilities sUtils = new MySwingUtilities();
    private WelcomeLoginPane welcomePane;
    public static double taux_du_jour = MagicData.getLastTaux();

    /** Creates new form MainForm */
    public MainForm() {

        initComponents();
        welcomePane = new WelcomeLoginPane(this);
        setTitle("Simple Compta 1.0");
        createTaskPane();
        loginMode();

        int w = Toolkit.getDefaultToolkit().getScreenSize().width;
        int h = Toolkit.getDefaultToolkit().getScreenSize().height;

        setSize(w, h);

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosed(WindowEvent e) {
                quit();
            }

            @Override
            public void windowClosing(WindowEvent e) {

                System.out.println("Sortie en douceur !");
                DBTool.getInstance().close();
                System.exit(0);
            }
        });
    }

    void createTaskPane() {

        hynewclient = new JXHyperlink(new_client_action);
        hynewclient.setText("Nouvelle dépense");
        hynewclient.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/Add-24_2.png")));
        tp_main.add(hynewclient);



        hydaillyreports = new JXHyperlink(dailly_report_action);
        hydaillyreports.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/Modify.png")));
        tp_reports.add(hydaillyreports);
        hydaillyreports.setText("Sous Totaux");

        hydaillyreports.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/Modify.png")));
        tp_reports.add(hydaillyreports);


         hy_recap = new JXHyperlink(recap_report_action);
         hy_recap.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/operations.png")));
          tp_reports.add( hy_recap);
         hy_recap.setText("Recapitulatif");

        hycategorie = new JXHyperlink(categories_actions);
        hycategorie.setText("catégories de dépenses");
        hycategorie.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/cay.png")));
        tp_admin.add(hycategorie);



        hyHistoriqueDepenses = new JXHyperlink(articles_action);
        hyHistoriqueDepenses.setText("Historique des dépenses");
        hyHistoriqueDepenses.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/magasin2.png")));
        tp_main.add(hyHistoriqueDepenses);


        hyclientsList = new JXHyperlink(client_list_action);
        hyclientsList.setText("Comptes utilisateurs");
        hyclientsList.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/Profile.png")));
        tp_admin.add(hyclientsList);


        hytaux = new JXHyperlink(taux_action);
        hytaux.setText("Taux du jour");
        hytaux.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/calculatrice.png")));
        tp_admin.add(hytaux);


       

        hycomptes = new JXHyperlink(comptes_action);
        hycomptes.setText("Grand Livre");
        hycomptes.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/Pie Chart-24.png")));
        tp_compta.add(hycomptes);

        hybalance = new JXHyperlink(balance_action);
        hybalance.setText("Balance Comptable");
        hybalance.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/Bar Chart24.png")));
        tp_compta.add(hybalance);



//--------------------------------------------------------------------------------------
        tp_admin.setCollapsed(true);
        tp_compta.setCollapsed(true);
        //    tp_stock.setCollapsed(true);

    }
    ///// ////////////////////////////////////////////////////////////////////////////////




             Action recap_report_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            recapMode();

        }
    };

    Action new_client_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            newDepenseMode();

        }
    };
    Action taux_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            tauxMode();
        }
    };
    Action categories_actions = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            categoriesMode();

        }
    };
    Action comptes_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            comptesMode();
            // System.out.println("Nouveau Client Cr�e");
        }
    };
    Action com_cloturees_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            //  comClotureMode();
            // System.out.println("Nouveau Client Cr�e");
        }
    };
    Action logout_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            if (hyHistoriqueDepenses.isEnabled()) {
                loginMode();
            } else {
                if (JOptionPane.showConfirmDialog(null, "Voulez-vous vraiment quitter?") == JOptionPane.YES_OPTION) {
                    System.exit(0);
                } else {
                    return;
                }
            }
        }
    };
    Action new_article_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            //    addArticleMode();
        }
    };
    Action dailly_report_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            ReportMode();
            System.out.println("Dailly report");
        }
    };
    Action client_list_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            userManagementMode();
        }
    };
    Action edit_models_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            // editModelsMode();
        }
    };
    Action articles_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            historiqueMode();
        }
    };
    Action balance_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            balanceMode();

        }
    };
    Action modeles_action = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            //modelesMode();
        }
    };

/////////////////////////////////////////////////////////////////////////////////////////////
    void balanceMode() {
    }

    void enableDesableHyperLinks(boolean enableOrDesable) {
        Component[] c = this.getComponents();
        for (int i = 0; i < c.length; i++) {
            if (c[i].getClass().equals(JXHyperlink.class)) {
                c[i].setEnabled(enableOrDesable);
            }
        }
    }

    void loginMode() {

        MySwingUtilities.setContentPane(parent_pane, welcomePane);

        jXTaskPaneContainer1.setVisible(false);
//        lab_login_as.setEnabled(false);
    }

    public void connectedMode(Login login) {


        int creditLevel = login.getCredit_level();

        switch (creditLevel) {

            case CreditLevel.ADMIN:
                tp_admin.setVisible(true);
                tp_compta.setVisible(false);
                tp_reports.setVisible(false);
                tp_main.setVisible(false);

                tp_admin.setCollapsed(false);
                break;

            case CreditLevel.SUPER:
                tp_admin.setVisible(true);
                tp_compta.setVisible(true);
                tp_reports.setVisible(true);
                tp_main.setVisible(true);
                break;

            case CreditLevel.DEPENSE_USER:
                tp_admin.setVisible(false);
                tp_compta.setVisible(false);
                tp_reports.setVisible(false);
                tp_main.setVisible(true);
                break;
            case CreditLevel.REPORT_USER:
                tp_admin.setVisible(false);
                tp_compta.setVisible(false);
                tp_reports.setVisible(true);
                tp_main.setVisible(false);
                break;
            case CreditLevel.COMPTA_USER:
                tp_admin.setVisible(false);
                tp_compta.setVisible(true);
                tp_reports.setVisible(false);
                tp_main.setVisible(false);
                tp_compta.setCollapsed(false);

                break;
        }

//        hyHistoriqueDepenses.setEnabled(true);
//        hyclientsList.setEnabled(true);
//        hydaillyreports.setEnabled(true);
//
//        lab_login_as.setEnabled(true);
//        hynewclient.setEnabled(true);


        defaultMode();

        jXTaskPaneContainer1.setVisible(true);

        mycontentpane.repaint();
        mycontentpane.revalidate();

        welcomePane.stopBusy();
        MainForm.login = login;
        lab_login_as.setText(login.getName());
    }

    void quit() {
        if (MySwingUtilities.showConfirmDialog("Voulez-vous vraiment quitter Simple Compta?") == JOptionPane.YES_OPTION) {
            DBTool.getInstance().close();
            System.exit(0);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXHeader1 = new org.jdesktop.swingx.JXHeader();
        mycontentpane = new javax.swing.JPanel();
        jXTaskPaneContainer1 = new org.jdesktop.swingx.JXTaskPaneContainer();
        tp_main = new org.jdesktop.swingx.JXTaskPane();
        tp_reports = new org.jdesktop.swingx.JXTaskPane();
        tp_admin = new org.jdesktop.swingx.JXTaskPane();
        tp_compta = new org.jdesktop.swingx.JXTaskPane();
        parent_pane = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        lab_login_as = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jXHeader1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tp_main.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        tp_main.setTitle("Dépenses");
        jXTaskPaneContainer1.add(tp_main);

        tp_reports.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        tp_reports.setTitle("Rapports");
        jXTaskPaneContainer1.add(tp_reports);

        tp_admin.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        tp_admin.setTitle("Administration");
        jXTaskPaneContainer1.add(tp_admin);

        tp_compta.setToolTipText("");
        tp_compta.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        tp_compta.setTitle("Comptabilité");
        jXTaskPaneContainer1.add(tp_compta);

        parent_pane.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout parent_paneLayout = new javax.swing.GroupLayout(parent_pane);
        parent_pane.setLayout(parent_paneLayout);
        parent_paneLayout.setHorizontalGroup(
            parent_paneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1036, Short.MAX_VALUE)
        );
        parent_paneLayout.setVerticalGroup(
            parent_paneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 429, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mycontentpaneLayout = new javax.swing.GroupLayout(mycontentpane);
        mycontentpane.setLayout(mycontentpaneLayout);
        mycontentpaneLayout.setHorizontalGroup(
            mycontentpaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mycontentpaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(parent_pane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        mycontentpaneLayout.setVerticalGroup(
            mycontentpaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mycontentpaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mycontentpaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(parent_pane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bash/jcouture/images/Exit.png"))); // NOI18N
        jButton1.setText("Quitter");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lab_login_as.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        lab_login_as.setForeground(new java.awt.Color(0, 51, 204));
        lab_login_as.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bash/jcouture/images/Profile.png"))); // NOI18N
        lab_login_as.setText("Utilisateur");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(lab_login_as, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(117, 117, 117)
                .addComponent(jButton1)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lab_login_as, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jButton1)))
                .addContainerGap())
        );

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel1.setBackground(new java.awt.Color(204, 204, 204));
        jLabel1.setFont(new java.awt.Font("Arial", 3, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 51, 51));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Simple Compta 1.0");
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXHeader1, javax.swing.GroupLayout.DEFAULT_SIZE, 1303, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(mycontentpane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXHeader1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator2)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mycontentpane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        quit();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator2;
    private org.jdesktop.swingx.JXHeader jXHeader1;
    private org.jdesktop.swingx.JXTaskPaneContainer jXTaskPaneContainer1;
    private javax.swing.JLabel lab_login_as;
    private javax.swing.JPanel mycontentpane;
    private javax.swing.JPanel parent_pane;
    private org.jdesktop.swingx.JXTaskPane tp_admin;
    private org.jdesktop.swingx.JXTaskPane tp_compta;
    private org.jdesktop.swingx.JXTaskPane tp_main;
    private org.jdesktop.swingx.JXTaskPane tp_reports;
    // End of variables declaration//GEN-END:variables

    private void defaultMode() {
        MySwingUtilities.setContentPane(parent_pane, new DefaultPane());
    }

    private void comptesMode() {
        MySwingUtilities.setContentPane(parent_pane, new ComptesPane());
    }

    private void categoriesMode() {
        MySwingUtilities.setContentPane(parent_pane, new CategoriesPane());
    }

    private void historiqueMode() {
        MySwingUtilities.setContentPane(parent_pane, new HistDepensesPane());
    }

    private void ReportMode() {
        MySwingUtilities.setContentPane(parent_pane, new BilansPane());
    }

    private void tauxMode() {
        MySwingUtilities.setContentPane(parent_pane, new TauxPane());
    }

    private void userManagementMode() {
        MySwingUtilities.setContentPane(parent_pane, new LoginManagePane());
    }

    private void newDepenseMode() {
        MySwingUtilities.setContentPane(parent_pane, new NewDepensePane());
    }

     private void  recapMode(){
        MySwingUtilities.setContentPane(parent_pane, new RecapPane());
    }

}
